package com.leetcode.search.dfs;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/7/25 22:58
 */
public class SortedArrayToBST_108 {

    public TreeNode sortedArrayToBST(int[] nums) {
        return nums.length == 0 ? null : buildTree(nums, 0, nums.length - 1);
    }

    public TreeNode buildTree(int[] nums, int l, int r) {
        if (l > r) return null;
        // 每次根节点都是通过中点来建立
        int mid = l + (r - l) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = buildTree(nums, l, mid - 1);
        node.right = buildTree(nums, mid + 1, r);
        return node;
    }

}
